using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Configuration;

namespace ProcessesSMS
{
    /// <summary>
    /// Summary description for.
    /// Project name: SMS_xoso
    /// channel: n/a
    /// command code: HN X Y
    /// created: 01/09/09 .
    /// Writer : Nampt + Tuanbc
    /// Company: VIT TELECOM
    /// </summary>

    public class XOSO_SOI
    {
        #region Declare
        private SMS_MO WMO;
        private SMS_MT WMT;
        ArrayList arr = new ArrayList();
        public XOSO_SOI(SMS_MO MO)
        {
            WMO = MO;
        }

        public XOSO_SOI()
        {
        }
        #endregion
        public void run()
        {
            WMT = new SMS_MT(WMO);
            string tmpMsg = WMO.Message.Substring(WMO.CommandCode.Length).Trim().Replace("  ", " ").Replace("  ", " ");
            tmpMsg = tmpMsg.Replace("  ", " ").Trim().ToUpper();
            string[] msg = tmpMsg.Split(new char[] { ' ' });
            string[] strReturnMsg = GetReturnMsg(msg).Split('|');
            for(int i=0;i<strReturnMsg.Length;i++)           
            {               
                WMT.AddMessage(WMO.UserID, strReturnMsg[i], SMS_ContentType.SMS_TEXT);
                WMT.MsgType = SMS_MsgType.SMS_TINHTIEN;                
            }
            WMT.Insert();
            return;
        }
        public string GetReturnMsg(string[] arr)
        {
            string msgBody = "", msgBody1 = "", msgBody2 = "";
            if ((WMO.ServiceID.Replace("04", "") == "7662") || (WMO.UserID == "84902222776") || (WMO.UserID == "84978925568"))
            {
                string strSQLDD;
                string strSQL;
                string connStr = ConfigurationSettings.AppSettings["CONNECTION_STRING_7x62"].ToString();
                SqlConnection conn = new SqlConnection(connStr);
                if (arr[0] == "TD" || arr[0] == "HN" || arr[0] == "QN" || arr[0] == "BN" || arr[0] == "HP" || arr[0] == "ND" || arr[0] == "TB" || arr[0] == "")
                {
                    strSQLDD = "select top 10 giaidacbiet, ngay from xoso where mamien='mb' order by ngay desc";
                    strSQL = "SELECT TOP 1 ManySpecAward, LessSpecAward, GuessSpecAward FROM XOSO_DD WHERE Mamien='MB'";
                }
                else
                {
                    strSQLDD = "select top 10 giaidacbiet, ngay from xoso where matinh='" + arr[0] + "' order by ngay desc";
                    strSQL = "SELECT Top 1 ManySpecAward, LessSpecAward, GuessSpecAward FROM XOSO_DD WHERE Matinh='" + arr[0] + "'";
                }
                //7 giai dac biet gan nhat
                conn.Open();
                SqlDataAdapter oda_c = new SqlDataAdapter(strSQLDD, conn);
                DataSet ods_c = new DataSet();
                oda_c.Fill(ods_c);
                DataTable odt = ods_c.Tables[0];
                conn.Close();
                int totalitem_c = odt.Rows.Count;
                if (totalitem_c > 0)
                {
                    for (int i = 0; i < totalitem_c; i++)
                    {
                        DateTime datetime = Convert.ToDateTime(odt.Rows[i]["Ngay"].ToString().Trim());
                        msgBody1 += odt.Rows[i]["GiaiDacBiet"].ToString().Trim() + "(" + datetime.Day + "/" + datetime.Month + ") ";
                    }
                    msgBody1 = "10 giai DB gan day nhat:" + msgBody1.Trim().Replace(" ", ",");
                }
                else
                {
                    return "MaTinh la cac chu cai dau ten tinh.Cac tinh ngoai le:DaNang: DNG,BinhDinh: BDH,DacLak: DLK,DacNong: DNO,QuangNam: DNM,QuangNgai: QNI,BenTre: BTR,BinhThuan: BTH";                     
                }               
                //GDB ve nhieu nhat 120 ngay va du doan lan toi     
                conn.Open();
                SqlDataAdapter da = new SqlDataAdapter(strSQL, conn);
                DataSet ds = new DataSet();
                da.Fill(ds);
                DataTable dt = ds.Tables[0];
                conn.Close();
                if (dt.Rows.Count > 0)
                {                   
                    msgBody2 = "GDB ve nhieu nhat trong 120 ngay:" + dt.Rows[0]["ManySpecAward"].ToString() + ".";
                    msgBody2 += "Du doan 2 so GDB lan toi:" + dt.Rows[0]["GuessSpecAward"].ToString();
                }                
            }
            else
            {
                msgBody2 = "SOI SO DAI LOC cung 7x62 soan: SOI MaTinh gui 7662.Vi du: De SOI SO DAI LOC kqxs mien Bac soan: SOI TD gui 7662. Chuc cac ban may man!";
            }
            msgBody = msgBody1 + "|" + msgBody2;
            return msgBody;
        }
    }
}
